package com.uwantsoft.business.service.domain.user;


import com.uwantsoft.business.facade.dto.address.FavoriteAddressesDto;
import com.uwantsoft.business.facade.dto.user.DriverDetailDto;
import com.uwantsoft.business.facade.dto.user.DriverLoginDto;
import com.uwantsoft.business.facade.dto.user.DriverOverviewDto;
import com.uwantsoft.business.facade.dto.user.LicensePlateDto;
import com.uwantsoft.business.service.domain.addresses.FavoriteAddress;
import com.uwantsoft.business.service.infrastructure.DriverRepository;
import com.uwantsoft.util.JodaUtils;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;

import javax.annotation.Resource;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LeiPeichen.
 */
@Entity
@Table(name = "driver")
@PrimaryKeyJoinColumn(name = "user_id")
@Configurable(autowire = Autowire.BY_NAME)
public class Driver extends User{

    @Resource(name = "hibernateDriverRepository")
    private transient DriverRepository hibernateDriverRepository;

    public static Driver createDriver(String mobileNumber) {
        Driver driver = new Driver();
        driver.username = mobileNumber;
        return driver;
    }

    public void login(DriverLoginDto driverLoginDto) {
        this.deviceType = driverLoginDto.getDeviceType();
        this.channelId = driverLoginDto.getChannelId();
        this.loginStatus = LoginStatus.ONLIEN;
    }

    public void createFavoriteAddress(FavoriteAddress favoriteAddress) {
        favoriteAddress.createFavoriteAddress(this);
        hibernateDriverRepository.save(favoriteAddress);
    }

    public List<FavoriteAddress> favoriteAddresses() {
        return hibernateDriverRepository.findFavoriteAddresses(this);
    }

    public void deleteFavoriteAddress(String guid) {
        FavoriteAddress favoriteAddress = hibernateDriverRepository.findByGuid(guid, FavoriteAddress.class);
        if(favoriteAddress != null){
            hibernateDriverRepository.delete(favoriteAddress);
        }
    }

    public void updateVerifyCode(String verifyCode) {
        this.password = verifyCode;
        this.loginStatus = LoginStatus.OFFLINE;
    }


    public List<LicensePlate> licensePlates() {
        return hibernateDriverRepository.findLicensePlates(this);
    }

    public void createLicensePlate(LicensePlate licensePlate) {
        if(licensePlate.priority()){
            LicensePlate priorityLicensePlate = hibernateDriverRepository.findPriorityLicensePlate();
            if(priorityLicensePlate != null){
                licensePlate.updatePriorityLicense(priorityLicensePlate);
            }
        }
        licensePlate.createLicensePlate(this);
        hibernateDriverRepository.save(licensePlate);
    }

    @Override
    public Role role() {
        return Role.DRIVER;
    }

    public void deleteLicensePlate(String guid) {
        LicensePlate licensePlate = hibernateDriverRepository.findByGuid(guid, LicensePlate.class);
        if(licensePlate != null){
            hibernateDriverRepository.delete(licensePlate);
        }
    }

    public DriverOverviewDto convertOverviewDto(){
        DriverOverviewDto overviewDto = new DriverOverviewDto();
        overviewDto.setGuid(this.guid());
        overviewDto.setUsername(this.username());
        overviewDto.setRegistrationDate(JodaUtils.formatDateTime(this.createTime()));
        overviewDto.setCurrentCity("成都市");
        overviewDto.setLoginStatus(this.loginStatus().getText());
        overviewDto.setActive(this.active());
        return overviewDto;
    }


    public DriverDetailDto convertDetailDto() {
        DriverDetailDto detailDto = new DriverDetailDto();
        detailDto.setGuid(this.guid());
        detailDto.setUsername(this.username());
        detailDto.setRegistrationDate(JodaUtils.formatDateTime(this.createTime()));
        detailDto.setCurrentCity("成都市");
        detailDto.setLoginStatus(this.loginStatus().getText());

        List<LicensePlate> licensePlates = licensePlates();
        List<LicensePlateDto> licensePlateDtos = new ArrayList<LicensePlateDto>();
        for (LicensePlate licensePlate : licensePlates) {
            licensePlateDtos.add(licensePlate.convert());
        }
        detailDto.setLicensePlates(licensePlateDtos);

        List<FavoriteAddress> favoriteAddresses = favoriteAddresses();
        List<FavoriteAddressesDto> favoriteAddressesDtos = new ArrayList<FavoriteAddressesDto>();
        for (FavoriteAddress favoriteAddress : favoriteAddresses) {
            favoriteAddressesDtos.add(favoriteAddress.convert());
        }
        detailDto.setFavoriteAddresses(favoriteAddressesDtos);
        return detailDto;
    }
    /**
     * only for unit test
     * @return
     */
    public static Driver testInstanceDriver(){
        Driver driver = new Driver();
        driver.username = "testusername";
        driver.password = "testuserpassword";
        driver.name = "test";
        return driver;
    }

    /**
     * only for unit test
     * @return
     */
    public static Driver testInstanceDriver(String username){
        Driver driver = new Driver();
        driver.username = username;
        driver.password = "testuserpassword";
        driver.name = "test";
        return driver;
    }
}
